package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Subject;
import com.atguigu.guli.service.edu.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-10-26
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/edu/subject")
public class AdminSubjectController {

    @Autowired
    SubjectService subjectService;

    @PostMapping("import")
    public R importSubjects(MultipartFile file){
        try {
            InputStream inputStream = file.getInputStream();
            subjectService.importSubjects(inputStream);
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/getNestedSubjects")
    public R getNestedSubjects(){

        List<Subject> nestedSubjects =
                subjectService.getNestedSubjects();

        return R.ok().data("items",nestedSubjects);
    }
}

